package com.shishi.fast.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author mm
 * @Date 2022/10/18 8:35
 * @Description TODO
 */
public class TimeCycleUtil {

    public static final int TIME_DAY = 1;

    public static final int TIME_WEEK = 2;

    public static final int TIME_MONTH = 3;

    public static final int TIME_QUARTER = 4;

    public static final int TIME_YEAR = 5;

    /**
     * 空构造
     */
    public TimeCycleUtil() {
    }

    /**
     * 调度函数
     *
     * @param date
     * @param dateDimension
     * @return
     */
    public static String schedulingFunction(String date, Integer dateDimension) {
        Integer flag = Integer.valueOf(date);
        switch (dateDimension) {
            case TIME_DAY:
                return getDayCycle(flag);
            case TIME_WEEK:
                return getWeekCycle(flag);
            case TIME_MONTH:
                return getMonthCycle(flag);
            case TIME_QUARTER:
                return getQuarterCycle(flag);
            case TIME_YEAR:
                return getYearCycle(flag);
            default:
                return "";
        }
    }

    /**
     * 获取上一日、本日、下一日
     *
     * @param flag
     * @return
     */
    public static String getDayCycle(Integer flag) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, flag);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    /**
     * 获取上周、本周、下周
     *
     * @param flag
     * @return
     */
    public static String getWeekCycle(Integer flag) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.add(Calendar.WEEK_OF_MONTH, flag);
        Date time = cal.getTime();
        String format = new SimpleDateFormat("yyyy-MM-dd").format(time);
        cal.add(Calendar.WEEK_OF_MONTH, 1);
        time = cal.getTime();
        return format + "," + new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    /**
     * 获取上月，本月，下月
     *
     * @param flag
     * @return
     */
    public static String getMonthCycle(Integer flag) {
        //初始月
        Calendar calStart = Calendar.getInstance();
        calStart.add(Calendar.MONTH, flag);
        Date timeStart = calStart.getTime();
        String formatStart = new SimpleDateFormat("yyyy-MM").format(timeStart);
        calStart.set(Calendar.DAY_OF_MONTH, calStart.getActualMaximum(Calendar.DAY_OF_MONTH));

        //初始月的下月
        Calendar calEnd = Calendar.getInstance();
        calEnd.add(Calendar.MONTH, flag + 1);
        Date timeEnd = calEnd.getTime();
        String formatEnd = new SimpleDateFormat("yyyy-MM").format(timeEnd);
        calEnd.set(Calendar.DAY_OF_MONTH, calEnd.getActualMaximum(Calendar.DAY_OF_MONTH));
        return formatStart + "-01," + formatEnd + "-01";
    }

    /**
     * 获取上季度，本季度，下季度
     *
     * @param flag
     * @return
     */
    public static String getQuarterCycle(Integer flag) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, ((int) cal.get(Calendar.MONTH) / 3) * 3);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int month = cal.get(Calendar.MONTH) + 1;
        int deviation = flag * 3;
        int sum = month + deviation;
        int quotient = sum / 12;
        if (sum < 0) {
            int i = month + Math.abs(quotient) * 12 + deviation;
            cal.add(Calendar.YEAR, quotient);
            cal.set(Calendar.MONTH, i - 1);
        } else {
            int i = month - Math.abs(quotient) * 12 + deviation;
            cal.add(Calendar.YEAR, quotient);
            cal.set(Calendar.MONTH, i - 1);
        }
        String format = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        cal.add(Calendar.MONTH, 3);
        return format + "," + new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    /**
     * 获取上一年，本年，下一年
     *
     * @param flag
     * @return
     */
    public static String getYearCycle(Integer flag) {
        Calendar cal = Calendar.getInstance();
        return (cal.get(Calendar.YEAR) + flag) + "-01-01," + (cal.get(Calendar.YEAR) + flag + 1) + "-01-01";
    }
}
